#include<bits/stdc++.h>
using namespace std;
const int maxm = 100000+7;
int stk[maxm], tot;
int main() {
  int n; cin>>n;
  while(n--) {
    string op; cin>>op;
    if("push"==op) {
      int x; cin>>x;
      stk[tot++] = x;
    }
    else if("query"==op) {
      cout<<stk[tot-1]<<'\n';
    }
    else if("pop"==op) {
      tot--;
    }
    else if("empty"==op) {
      cout<<(tot==0?"YES":"NO")<<'\n';
    }
    else {
      cout<<"OP is '"<<op<<"'\n";
      assert(false);
    }
  }
}
